package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Student;
import com.rzj.sys_lab_assistant.entity.StudentApply;
import com.rzj.sys_lab_assistant.service.IStudentApplyService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-02 08:20
 */
@Controller
@RequestMapping("studentApply")
public class StudentApplyController {

    @Autowired
    IStudentApplyService studentApplyService;

    private ResultsUtils resultsUtils;

    @RequestMapping("toAdd/{postId}")
    public String toAdd(@PathVariable("postId") int postId, Model model) {
        model.addAttribute("postId", postId);
        return "studentApply/studentApplyAdd";
    }

    @RequestMapping("toEdit/{studentNo}")
    public String toEdit(Model model, @PathVariable("studentNo") String studentNo) {
        model.addAttribute("studentApply", studentApplyService.getStudentApplyByNo(studentNo));
        return "studentApply/studentApplyUpdate";
    }

    @RequestMapping("studentApplyList/{postId}")
    public String studentApplyList(Model model, @PathVariable("postId") int postId) {
        model.addAttribute("studentApplyList", studentApplyService.studentAppliesList(postId));
        return "studentApply/studentApplyList";
    }

    @RequestMapping("studentApplyListByNo")
    public String studentApplyListByNo(Model model, HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        model.addAttribute("studentApplyList", studentApplyService.studentAppliesByNoList(student.getStudentNo())); // 根据学生登录 获取session 进行查询条件
        return "studentApply/studentApplyListByNo";
    }

    @RequestMapping("studentApplyAdd")
    @ResponseBody
    public Map<String, String> studentApplyAdd(@RequestParam("postId") String postId,
                                               @RequestParam("jobDescription") String jobDescription,
                                               @RequestParam("studentNo") String studentNo,
                                               @RequestParam("applyName") String applyName) {
        StudentApply studentApply = new StudentApply(0, Integer.valueOf(postId), jobDescription, studentNo, applyName, 0);

        int flag = studentApplyService.addStudentApply(studentApply);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }
    }

    @RequestMapping("studentApplyEdit")
    @ResponseBody
    public Map<String, String> studentApplyEdit(@RequestParam("id") String id,
                                                @RequestParam("postId") String postId,
                                                @RequestParam("jobDescription") String jobDescription,
                                                @RequestParam("studentNo") String studentNo,
                                                @RequestParam("applyName") String applyName) {
        StudentApply studentApply = new StudentApply(Integer.valueOf(id), Integer.valueOf(postId), jobDescription, studentNo, applyName, 0);

        int flag = studentApplyService.updateStudentApply(studentApply);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("studentApplyStatusEdit")
    @ResponseBody
    public Map<String, String> studentApplyStatusEdit(@RequestParam("id") String id,
                                                @RequestParam("applyType") String applyType) {
        StudentApply studentApply = new StudentApply();
        studentApply.setId(Integer.valueOf(id));
        studentApply.setApplyType(Integer.valueOf(applyType));

        int flag = studentApplyService.updateStudentApplyStatus(studentApply);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }
    }

    @RequestMapping("studentApplyCheck")
    @ResponseBody
    public Map<String, String> studentApplyCheck(HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        StudentApply studentApply = studentApplyService.getStudentApplyByNo(student.getStudentNo()); // 从学生登录对象中取学生学号信息
        if (studentApply == null || studentApply.getApplyType() == 2) {
            return resultsUtils.resultsMap("success", "可报名！");
        } else {
            return resultsUtils.resultsMap("error", "存在报名信息！");
        }
    }

    @RequestMapping("studentApplyDel")
    @ResponseBody
    public Map<String, String> studentApplyDel(@RequestParam("id") String id) {

        int flag = studentApplyService.delStudentApply(Integer.valueOf(id));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }


}
